package com.sinosoft.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.ModelAndView;

import com.sinosoft.base.BaseFormController;
import com.sinosoft.base.Constants;
import com.sinosoft.data1.ZywxImage1Manager;
import com.sinosoft.product.ZywxImage1;
import com.sinosoft.util.DateTimeUtils;
import com.sinosoft.util.DisplayGetPage;

@Controller
@RequestMapping("/zywximage/zywximageEdit.html")
public class ZywxImage1Controller extends BaseFormController {

	private ZywxImage1Manager zgr = null;

	@Autowired
	public void setZgr(ZywxImage1Manager zgr) {
		this.zgr = zgr;
	}

	// 列表显示
	@RequestMapping(params = "method=list", method = RequestMethod.GET)
	public ModelAndView select(Model model, HttpServletRequest request) {
		String page = DisplayGetPage.getPageParamName("zywxImage1", request);// 第几页
		ZywxImage1 zywx1 = new ZywxImage1();
		List zywx1List = null;
		int pagesize = zgr.getPagesize(zywx1);

		zywx1List = zgr.getZywxImage1List((Integer.parseInt(page) - 1)
				* Constants.pagesize, Constants.pagesize, zywx1);
		model.addAttribute("resultSize", pagesize);

		double size = Constants.pagesize;
		model.addAttribute("displayallpage", Math.ceil(pagesize / size));
		model.addAttribute("thispage", Integer.parseInt(page));

		ZywxImage1 zyIm = new ZywxImage1();
		model.addAttribute("zywxImage1", zyIm);

		model.addAttribute("ptmeth", "list");

		return new ModelAndView("zywximage/zywxImage1List",
				Constants.LEVEL_1_MODEL, zywx1List);

	}

	// 删除操作
	@RequestMapping(params = "method=delete", method = RequestMethod.GET)
	public String delete(String productid, HttpServletRequest request) {

		zgr.removeZywxImage1(productid);

		String page = request.getParameter("tz");
		String pagename = DisplayGetPage.getPageName("zywxImage1", request);

		String postmehtod = request.getParameter("ptmeth");

		return "redirect:/zywximage/zywximageEdit.html?method=list&" + pagename
				+ "=" + page;

	}

	// 进入编辑页面
	@RequestMapping(params = "method=inedit")
	public String showForm(String productid, HttpServletRequest request,
			Model model, HttpServletResponse response) throws Exception {

		ZywxImage1 zywx1 = new ZywxImage1();

		if ("null".equals(productid) || null == productid) {

		} else {
			zywx1 = zgr.getZywxImage1ById(productid);
		}
		// 将翻页参数和查询方式传到edit页面
		String page = request.getParameter("tz");
		String pagename = DisplayGetPage.getPageName("zywxImage1", request);

		model.addAttribute("tzpage", page);
		model.addAttribute("pagename", pagename);
		model.addAttribute("ptmeth", request.getParameter("ptmeth"));
		model.addAttribute("ZywxImage1", zywx1);
		
		return "zywximage/zywximageEdit";
		// return zywx1;
	}

	// 添加修改的方法
	@RequestMapping(params = "method=edit")
	public String edit(ZywxImage1 zywx1, HttpServletRequest request) {

		try {
			zgr.saveZywxImage1(zywx1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		// ApplicationContext ctx = WebApplicationContextUtils
		// .getRequiredWebApplicationContext(getServletContext());
		// System.out.println("2");
		// ZywxImage1Manager zgr = (ZywxImage1Manager) ctx
		// .getBean("ZywxImage1Manager");
		// System.out.println("33");
		// getServletContext().setAttribute(Constants.LEVEL_1_MODEL,
		// zgr.getZywxImage1());
		String pagename = request.getParameter("pagename");
		String page = request.getParameter("tz");
		if("".equalsIgnoreCase(page)){
			page = "1";
		}
		String postmethod = request.getParameter("ptmeth");
		return "redirect:/zywximage/zywximageEdit.html?method=list&"+ pagename + "=" + page;

	}
	
	
	//进入详细查看
	
	@RequestMapping(params = "method=ineditXq")
	public String showFormXq(String productid, HttpServletRequest request,
			Model model, HttpServletResponse response) throws Exception {

		ZywxImage1 zywx1 = new ZywxImage1();

		if ("null".equals(productid) || null == productid) {

		} else {
			zywx1 = zgr.getZywxImage1ById(productid);
		}
		// 将翻页参数和查询方式传到edit页面
		String page = request.getParameter("tz");
		String pagename = DisplayGetPage.getPageName("zywxImage1", request);

		model.addAttribute("tzpage", page);
		model.addAttribute("pagename", pagename);
		model.addAttribute("ptmeth", request.getParameter("ptmeth"));
		model.addAttribute("ZywxImage1", zywx1);
		
		return "zywximage/zywx1CKList";
	}
}


